/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.synyx.hades.domain.AbstractPersistable;

import com.googlecode.chigscommons.domain.User;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
	@NamedQuery(name = "Revision.findByAll", query = "from Revision o"),
	@NamedQuery(name = "Revision.findByModuleLatest", query = "from Revision o where o.module = ? and o.nr = (select max(rev.nr) from Revision rev where rev.module = o.module)"),
	@NamedQuery(name = "Revision.findByConfigRevision", query = "select o from Revision o, in (o.configRevisions) c where c = ?")})
public class Revision extends AbstractPersistable<Long> {
	private int nr;
	private String description;
	
	@ManyToOne
	private User user;

	@ManyToOne
	private Module module;
	
	@ManyToOne
	private Version version;
	
	@ManyToMany
    @JoinTable(name="Revision_Module",
            joinColumns=@JoinColumn(name="revision_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="module_id", referencedColumnName="id"))
	private List<Module> dependencies;
	
	@ManyToMany
    @JoinTable(name="Revision_Annotation",
            joinColumns=@JoinColumn(name="revision_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="annotation_id", referencedColumnName="id"))
	private List<Annotation> annotations;

	@ManyToMany
    @JoinTable(name="Revision_GenericScript",
            joinColumns=@JoinColumn(name="revision_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="genericScript_id", referencedColumnName="id"))
	private List<GenericScript> genericScripts;

	@ManyToMany
    @JoinTable(name="Revision_ConfigPair",
            joinColumns=@JoinColumn(name="revision_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="configPair_id", referencedColumnName="id"))
	private List<ConfigPair> configPairs;

	@ManyToMany
    @JoinTable(name="Revision_Attachment",
            joinColumns=@JoinColumn(name="revision_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="attachment_id", referencedColumnName="id"))
	private List<Attachment> attachments;

	@ManyToMany(mappedBy="revisions")
	private final List<ConfigRevision> configRevisions;
	
	public Revision() {
		super();
		this.description = "";
        this.module = new Module();
		this.version = new Version();
		this.dependencies = new ArrayList<Module>();		
		this.annotations = new ArrayList<Annotation>();
		this.genericScripts = new ArrayList<GenericScript>();
		this.configRevisions = new ArrayList<ConfigRevision>();
		this.configPairs = new ArrayList<ConfigPair>();
		this.attachments = new ArrayList<Attachment>();
	}

	public int getNr() {
		return nr;
	}

	public void setNr(int nr) {
		this.nr = nr;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setVersion(Version version) {
		this.version = version;
	}

	public Version getVersion() {
		return version;
	}

	public void setDependencies(List<Module> dependencies) {
		this.dependencies = dependencies;
	}

	public List<Module> getDependencies() {
		return dependencies;
	}

	public List<Annotation> getAnnotations() {
		return annotations;
	}

	public void setAnnotations(List<Annotation> annotations) {
		this.annotations = annotations;
	}

	public List<GenericScript> getGenericScripts() {
		return genericScripts;
	}

	public void setGenericScripts(List<GenericScript> genericScripts) {
		this.genericScripts = genericScripts;
	}

	public List<ConfigPair> getConfigPairs() {
		return configPairs;
	}

	public void setConfigPairs(List<ConfigPair> configPairs) {
		this.configPairs = configPairs;
	}

	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	@Override
	public String toString() {
		return String
				.format(
						"Revision [description=%s, module=%s, nr=%s, user=%s, version=%s, getId()=%s]",
						description, module, nr, user, version,	getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((annotations == null) ? 0 : annotations.hashCode());
		result = prime * result
				+ ((attachments == null) ? 0 : attachments.hashCode());
		result = prime
				* result
				+ ((attachments == null) ? 0 : attachments
						.hashCode());
		result = prime * result
				+ ((configPairs == null) ? 0 : configPairs.hashCode());
		result = prime * result
				+ ((configRevisions == null) ? 0 : configRevisions.hashCode());
		result = prime * result
				+ ((dependencies == null) ? 0 : dependencies.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result
				+ ((genericScripts == null) ? 0 : genericScripts.hashCode());
		result = prime * result + ((module == null) ? 0 : module.hashCode());
		result = prime * result + nr;
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		result = prime * result + ((version == null) ? 0 : version.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof Revision)) {
			return false;
		}
		Revision other = (Revision) obj;
		if (annotations == null) {
			if (other.annotations != null) {
				return false;
			}
		} else if (!annotations.equals(other.annotations)) {
			return false;
		}
		if (attachments == null) {
			if (other.attachments != null) {
				return false;
			}
		} else if (!attachments.equals(other.attachments)) {
			return false;
		}
		if (attachments == null) {
			if (other.attachments != null) {
				return false;
			}
		} else if (!attachments.equals(other.attachments)) {
			return false;
		}
		if (configPairs == null) {
			if (other.configPairs != null) {
				return false;
			}
		} else if (!configPairs.equals(other.configPairs)) {
			return false;
		}
		if (configRevisions == null) {
			if (other.configRevisions != null) {
				return false;
			}
		} else if (!configRevisions.equals(other.configRevisions)) {
			return false;
		}
		if (dependencies == null) {
			if (other.dependencies != null) {
				return false;
			}
		} else if (!dependencies.equals(other.dependencies)) {
			return false;
		}
		if (description == null) {
			if (other.description != null) {
				return false;
			}
		} else if (!description.equals(other.description)) {
			return false;
		}
		if (genericScripts == null) {
			if (other.genericScripts != null) {
				return false;
			}
		} else if (!genericScripts.equals(other.genericScripts)) {
			return false;
		}
		if (module == null) {
			if (other.module != null) {
				return false;
			}
		} else if (!module.equals(other.module)) {
			return false;
		}
		if (nr != other.nr) {
			return false;
		}
		if (user == null) {
			if (other.user != null) {
				return false;
			}
		} else if (!user.equals(other.user)) {
			return false;
		}
		if (version == null) {
			if (other.version != null) {
				return false;
			}
		} else if (!version.equals(other.version)) {
			return false;
		}
		return true;
	}
}
